package com.spc.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qiniu.util.Json;
import com.spc.constant.MessageConstant;
import com.spc.entity.PageResult;
import com.spc.entity.QueryPageBean;
import com.spc.entity.Result;
import com.spc.pojo.CheckGroup;
import com.spc.pojo.Menu;
import com.spc.pojo.Role;
import com.spc.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    //套餐分页查项
    @RequestMapping("/getUsertitle")
    public Result getUsername(){
        //获取上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null){
            //没有登录
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
        String username = user.getUsername();

        return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,username);
    }
	    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
          PageResult pageResult = userService.findPage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            com.spc.pojo.User user = userService.findById(id);
            return new Result(true,MessageConstant.GET_USER_DETAILE_REPORT_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USER_DETAILE_REPORT_FAIL);
        }
    }
    @RequestMapping("/add")
    public Result add(@RequestBody com.spc.pojo.User user){
        try {
            userService.add(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }

    /**
     * 用户基本数据修改
     * @param user
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.spc.pojo.User user){
        try {
            userService.edit(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }


    @RequestMapping("/editRoles")
    public Result editRoles(Integer id,@RequestBody Integer[] RoleIds){
        try {
            userService.editRoles(id,RoleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }

    @RequestMapping("/findRoles")
    public Result findRoles(){
        try {
            List<Role> list = userService.findRoles();
            return  new Result(true,MessageConstant.GET_USER_DETAILE_REPORT_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.GET_USER_DETAILE_REPORT_FAIL);
        }
    }
    @RequestMapping("/findUserRoles")
    public Result findUserRoles(Integer id){
        try {
            Integer[] integers = userService.findUserRoles(id);
            return  new Result(true,MessageConstant.GET_USER_DETAILE_REPORT_SUCCESS,integers);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.GET_USER_DETAILE_REPORT_FAIL);
        }
    }

    @RequestMapping("/getmenu")
    public Result getmenu(){
        //获取上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null){
            //没有登录,获取菜单失败
            return new Result(false,MessageConstant.GET_MEMU_FAIL);
        }
        String username = user.getUsername();
        //拿到用户名后查询数据库,拿到对应的角色菜单数据来动态显示菜单
        try {
            List<Menu> menus = userService.findByname(username);

            return new Result(true,MessageConstant.GET_MEMU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMU_FAIL);
        }
    }
}
